package com.bwie;

import org.junit.jupiter.api.Test;
import org.redisson.MapWriterTask;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: zhaijiaqi
 * @Date: 2025/6/28 11:35
 * @Description:
 **/
public class SynchronizedBook {

    private List<Book> bookList = new ArrayList<>();

    //添加
    public synchronized Book addBook(Book book) {
        bookList.add(book);
        System.out.println(book);
        return book;
    }

    //删除
    public synchronized Book removeBook(int index) {
        System.out.println(index);
        return bookList.remove(index);
    }

    //获取
    public synchronized Book getBook(int index) {
        System.out.println(index);
        return bookList.get(index);
    }

    public void sout() {
        for (Book book : bookList) {
            System.out.println(
                    book
            );
        }
    }

    @Test
   public void getListBook(){
       SynchronizedBook synchronizedBook = new SynchronizedBook();
       synchronizedBook.addBook(new Book(1,"title1","author1","publisher1"));
       synchronizedBook.addBook(new Book(2,"title2","author2","publisher2"));
       synchronizedBook.addBook(new Book(3,"title3","author3","publisher3"));
       System.out.println("添加");
       synchronizedBook.removeBook(1);
       System.out.println("删除");
       synchronizedBook.getBook(1);
       System.out.println("获取");
       synchronizedBook.sout();
   }




}
